package cn.classg.xxl.job.service.impl;

import cn.classg.xxl.job.model.XxlJobGroup;
import cn.classg.xxl.job.properties.XxlJobProperties;
import cn.classg.xxl.job.service.JobGroupService;
import cn.classg.xxl.job.service.JobLoginService;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * job 分组服务
 * @author CLASSG
 */
@Service
public class JobGroupServiceImpl implements JobGroupService {

    private JobLoginService jobLoginService;

    private XxlJobProperties xxlJobProperties;

    public JobGroupServiceImpl(JobLoginService jobLoginService, XxlJobProperties xxlJobProperties) {
        this.jobLoginService = jobLoginService;
        this.xxlJobProperties = xxlJobProperties;
    }

    @Override
    public List<XxlJobGroup> getJobGroup() {
        String url=xxlJobProperties.getAddresses()+"/jobgroup/pageList";
        HttpResponse response = HttpRequest.post(url)
                .form("appname", xxlJobProperties.getAppname())
                .form("title", xxlJobProperties.getTitle())
                .cookie(jobLoginService.getCookie())
                .execute();

        String body = response.body();
        JSONArray array = JSONUtil.parse(body).getByPath("data", JSONArray.class);
        List<XxlJobGroup> list = array.stream()
                .map(o -> JSONUtil.toBean((JSONObject) o, XxlJobGroup.class))
                .collect(Collectors.toList());

        return list;
    }

    @Override
    public boolean autoRegisterGroup() {
        String url=xxlJobProperties.getAddresses()+"/jobgroup/save";
        HttpRequest httpRequest = HttpRequest.post(url)
                .form("appname", xxlJobProperties.getAppname())
                .form("title", xxlJobProperties.getTitle());

        httpRequest.form("addressType",xxlJobProperties.getAddressType());
        if (xxlJobProperties.getAddressType().equals(1)){
            if (Strings.isBlank(xxlJobProperties.getAddressList())){
                throw new RuntimeException("手动录入模式下,执行器地址列表不能为空");
            }
            httpRequest.form("addressList",xxlJobProperties.getAddressList());
        }

        HttpResponse response = httpRequest.cookie(jobLoginService.getCookie())
                .execute();
        Object code = JSONUtil.parse(response.body()).getByPath("code");
        return code.equals(200);
    }

    @Override
    public boolean preciselyCheck() {
        List<XxlJobGroup> jobGroup = getJobGroup();
        Optional<XxlJobGroup> has = jobGroup.stream()
                .filter(xxlJobGroup -> xxlJobGroup.getAppname().equals(xxlJobProperties.getAppname())
                        && xxlJobGroup.getTitle().equals(xxlJobProperties.getTitle()))
                .findAny();
        return has.isPresent();
    }

}
